package com.apes.framework.jpa.logger;


import com.apes.framework.jpa.repository.CustomRepository;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


/**
 * @author xul
 * @create 2021-09-01 10:09
 */
public interface ApesRelationRepository extends CustomRepository<ApesRelation, Long> {

    @Query(value = "SELECT T.*, LEVEL " +
            "       FROM IR_RELATION T WHERE LEVEL = 1 " +
            "       START WITH T.BUSINESS_KEY = :businessKey " +
            "       CONNECT BY PRIOR T.BUSINESS_KEY = T.SOURCE_BUSINESS_KEY",nativeQuery = true)
    List<ApesRelation> findAllByBusinessKeyLevelOne(String businessKey);

    @Query(value = "SELECT T.*, LEVEL " +
            "       FROM IR_RELATION T  " +
            "       START WITH T.BUSINESS_KEY = :businessKey " +
            "       CONNECT BY PRIOR T.BUSINESS_KEY = T.SOURCE_BUSINESS_KEY",nativeQuery = true)
    List<ApesRelation> findAllByBusinessKey(String businessKey);

    @Query(value = "SELECT T.*, LEVEL " +
            "       FROM IR_RELATION T WHERE LEVEL = 2 " +
            "       START WITH T.BUSINESS_KEY = :businessKey AND T.BUSINESS_TYPE = :businessType" +
            "       CONNECT BY PRIOR T.BUSINESS_KEY = T.SOURCE_BUSINESS_KEY",nativeQuery = true)
    List<ApesRelation> findAllByBusinessKeyAndBusinessType(String businessKey, String businessType);

    @Query(value = "SELECT T.*, LEVEL " +
            "       FROM IR_RELATION T WHERE LEVEL = 1 " +
            "       START WITH T.BUSINESS_KEY in :businessKeys " +
            "       CONNECT BY PRIOR T.BUSINESS_KEY = T.SOURCE_BUSINESS_KEY ",nativeQuery = true)
    List<ApesRelation> findAllByBusinessKeysLevelOne(List businessKeys);
}
